package com.sneakxy.cloudbase.platform.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sneakxy.cloudbase.platform.bean.Account;
import com.sneakxy.cloudbase.platform.bean.Menu;
import com.sneakxy.cloudbase.platform.bean.ValidAdd;
import com.sneakxy.cloudbase.platform.bean.ValidEdit;
import com.sneakxy.cloudbase.platform.bean.dto.MenuDTO;
import com.sneakxy.cloudbase.platform.bean.dto.MenuTreeDTO;
import com.sneakxy.cloudbase.platform.service.IMenuService;
import com.sneakxy.cloudbase.platform.utils.Constants;
import com.sneakxy.cloudbase.platform.utils.web.BeanHelper;
import com.sneakxy.cloudbase.platform.utils.web.Response;
import com.sneakxy.cloudbase.platform.utils.web.signature.RequireSignature;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags="菜单服务接口")
@RestController
@RequestMapping("/api/menu/")
public class MenuController extends BaseController {
	
	@Autowired
	private IMenuService menuService;
	
	@ApiOperation("查询菜单树信息")
	@RequireSignature
	@RequiresPermissions("sys:menu:manager")
	@PostMapping("tree")
	public Response<MenuTreeDTO> tree(
			@ApiParam(name="type", value="菜单类型", required=true)
			@RequestParam(name="type")
			String type) {
		MenuTreeDTO result = new MenuTreeDTO();
		List<MenuDTO> list = menuService.findTreeByType(StringUtils.trimToEmpty(type));
		result.setId(type);
		result.setI18n("sys.menu.manager");
		result.setOpen(true);
		result.setChildren(list);
		return Response.ok(result);
	}
	
	@ApiImplicitParams({
		@ApiImplicitParam(name="icon", value="菜单图标", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="router", value="菜单路由", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="name", value="菜单名称", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="permission", value="权限标志", dataType="String", dataTypeClass = String.class, paramType="query", required=false),
		@ApiImplicitParam(name="sort", value="菜单排序", dataType="int", dataTypeClass = Integer.class, paramType="query", required=true),
		@ApiImplicitParam(name="i18n", value="菜单名国际化", dataType="String", dataTypeClass = String.class, paramType="query", required=false),
		@ApiImplicitParam(name="type", value="菜单类型", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="enable", value="是否开启", dataType="boolean", dataTypeClass = Boolean.class, paramType="query", required=true),
		@ApiImplicitParam(name="menuType", value="响应类型", dataType="String", dataTypeClass = String.class, paramType="query", required=true)
	})
	@ApiOperation("添加菜单树信息")
	@RequireSignature
	@RequiresPermissions("sys:menu:manager:add")
	@PostMapping("add")
	public Response<MenuDTO> add(
			@Validated(ValidAdd.class)
			@ApiIgnore 
			MenuDTO dto) {
		BeanHelper.trimToNull(dto);
		
		Menu menu = MenuDTO.to(dto);
		menu = menuService.save(menu);
		
		return Response.ok(MenuDTO.from(menu));
	}
	
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="菜单ID", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="icon", value="菜单图标", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="router", value="菜单路由", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="name", value="菜单名称", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="permission", value="权限标志", dataType="String", dataTypeClass = String.class, paramType="query", required=false),
		@ApiImplicitParam(name="sort", value="菜单排序", dataType="int", dataTypeClass = Integer.class, paramType="query", required=true),
		@ApiImplicitParam(name="i18n", value="菜单名国际化", dataType="String", dataTypeClass = String.class, paramType="query", required=false),
		@ApiImplicitParam(name="enable", value="是否开启", dataType="boolean", dataTypeClass = Boolean.class, paramType="query", required=true),
		@ApiImplicitParam(name="menuType", value="响应类型", dataType="String", dataTypeClass = String.class, paramType="query", required=true)
	})
	@ApiOperation("菜单树信息修改")
	@RequireSignature
	@RequiresPermissions("sys:menu:manager:edit")
	@PostMapping("edit")
	public Response<MenuDTO> edit(
			@Validated(ValidEdit.class)
			@ApiIgnore 
			MenuDTO dto) {
		BeanHelper.trimToEmpty(dto);
		
		Menu menu = MenuDTO.to(dto);
		menu.setCreatetime(null);
		menu.setType(null);
		menu = menuService.save(menu);
		
		return Response.ok(MenuDTO.from(menu));
	}
	
	@ApiOperation("菜单树信息开启/关闭")
	@RequireSignature
	@RequiresPermissions("sys:menu:manager:edit")
	@PostMapping("enable")
	public Response<String> enable(
			@ApiParam(name="id", value="菜单ID", required=true)
			@RequestParam(name="id")
			String id,
			@ApiParam(name="enable", value="是否开启", required=true)
			@RequestParam(name="enable")
			boolean enable) {
		String menuId = StringUtils.trimToEmpty(id);
		if(StringUtils.isNotBlank(menuId)) {
			Menu menu = new Menu();
			menu.setId(menuId);
			menu.setEnable(enable);
			menuService.save(menu);
		}
		return Response.ok();
	}
	
	@ApiOperation("菜单树信息删除")
	@RequireSignature
	@RequiresPermissions("sys:menu:manager:remove")
	@PostMapping("remove")
	public Response<String> remove(
			@ApiParam(name="id", value="菜单ID", required=true)
			@RequestParam(name="id")
			String id) {
		String menuId = StringUtils.trimToEmpty(id);
		if(StringUtils.isNotBlank(menuId) && 
				menuService.remove(menuId)) {
			return Response.ok();
		}
		return Response.fail(1);
	}
	
	@ApiOperation("查询当前菜单树信息")
	@PostMapping("menus")
	public Response<List<MenuDTO>> menus(
			@ApiParam(name="type", value="菜单类型", required=true)
			@RequestParam(name="type")
			String type) {
		Account account = (Account) getLoginAccount();
		List<MenuDTO> menus = menuService.findByAccountId(
				account != null ? account.getId() : Constants.NONE_ACCOUNT_ID, type);
		return Response.ok(menus);
	}
	
	@ApiOperation("查询单个菜单信息")
	@RequireSignature
	@PostMapping("one")
	public Response<MenuDTO> one(
			@ApiParam(name="id", value="菜单ID", required=true)
			@RequestParam(name="id")
			String id) {
		Menu menu = menuService.findOne(id);
		return Response.ok(menu, MenuDTO::from);
	}
	
}
